<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Add an update operation to the bulk</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb-driver-bulkwrite.insert.html">« MongoDB\Driver\BulkWrite::insert</a></li>
      <li style="float: right;"><a href="class.mongodb-driver-writeconcern.html">MongoDB\Driver\WriteConcern »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-driver-bulkwrite.html">MongoDB\Driver\BulkWrite</a></li>
    <li>Add an update operation to the bulk</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-driver-bulkwrite.update" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\BulkWrite::update</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\BulkWrite::update</span> &mdash; <span class="dc-title">Add an update operation to the bulk</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-bulkwrite.update-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\BulkWrite::update</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter">$filter</code></span>, <span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter">$newObj</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$updateOptions</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   Adds an update operation to the
   <span class="classname"><a href="class.mongodb-driver-bulkwrite.html" class="classname">MongoDB\Driver\BulkWrite</a></span>.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-bulkwrite.update-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
   
    <dt>
<code class="parameter">filter</code> (<span class="type"><span class="type">array</span>|<span class="type">object</span></span>)</dt>

    <dd>

     <p class="para">
      The <a href="https://www.mongodb.com/docs/manual/tutorial/query-documents/" class="link external">&raquo;&nbsp;query predicate</a>.
      An empty predicate will match all documents in the collection.
     </p>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <span class="simpara">
       When evaluating query criteria, MongoDB compares types and values according to its own <a href="https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/" class="link external">&raquo;&nbsp;comparison rules for BSON types</a>, which differs from PHP&#039;s <a href="types.comparisons.html" class="link">comparison</a> and <a href="language.types.type-juggling.html" class="link">type juggling</a> rules. When matching a special BSON type the query criteria should use the respective <a href="book.bson.html" class="link">BSON class</a> (e.g. use <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> to match an <a href="https://www.mongodb.com/docs/manual/reference/bson-types/#objectid" class="link external">&raquo;&nbsp;ObjectId</a>).
      </span>
     </p></blockquote>
    </dd>

   

   
    <dt>
<code class="parameter">newObj</code> (<span class="type"><span class="type">array</span>|<span class="type">object</span></span>)</dt>

    <dd>

     <p class="para">
      A document containing either update operators (e.g.
      <code class="literal">$set</code>), a replacement document (i.e.
      <em class="emphasis">only</em> <code class="literal">field:value</code> expressions), or
      an <a href="https://www.mongodb.com/docs/manual/reference/command/update/#update-with-an-aggregation-pipeline" class="link external">&raquo;&nbsp;aggregation pipeline</a>.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">updateOptions</code></dt>

    <dd>

     <p class="para">
      <table class="doctable table">
       <caption><strong>updateOptions</strong></caption>
       
        <thead>
         <tr>
          <th>Option</th>
          <th>Type</th>
          <th>Description</th>
          <th>Default</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>arrayFilters</td>
          <td><span class="type">array</span></td>
          <td>
           <p class="para">
            An array of filter documents that determines which array elements to
            modify for an update operation on an array field. See
            <a href="https://www.mongodb.com/docs/manual/reference/command/update/#update-command-arrayfilters" class="link external">&raquo;&nbsp;Specify arrayFilters for Array Update Operations</a>
            in the MongoDB manual for more information.
           </p>
           <p class="para">
            This option is available in MongoDB 3.6+ and will result in an
            exception at execution time if specified for an older server
            version.
           </p>
          </td>
         </tr>

         
         <tr>
          <td>collation</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            <a href="https://www.mongodb.com/docs/upcoming/reference/collation/" class="link external">&raquo;&nbsp;Collation</a> allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the <code class="literal">&quot;locale&quot;</code> field is mandatory; all other collation fields are optional. For descriptions of the fields, see <a href="https://www.mongodb.com/docs/upcoming/reference/collation/#collation-document" class="link external">&raquo;&nbsp;Collation Document</a>.
           </p>
           <p class="para">
            If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.
           </p>
           <p class="para">
            This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.
           </p>
          </td>
         </tr>


         <tr>
          <td>hint</td>
          <td><span class="type"><span class="type">string</span>|<span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            Index specification. Specify either the index name as a string or
            the index key pattern. If specified, then the query system will only
            consider plans using the hinted index.
           </p>
           <p class="para">
            This option is available in MongoDB 4.2+ and will result in an
            exception at execution time if specified for an older server
            version.
           </p>
          </td>
         </tr>

         <tr>
          <td>multi</td>
          <td><span class="type">bool</span></td>
          <td>
           Update only the first matching document if <strong><code>false</code></strong>, or all
           matching documents <strong><code>true</code></strong>. This option cannot be <strong><code>true</code></strong> if
           <code class="parameter">newObj</code> is a replacement document.
          </td>
          <td><strong><code>false</code></strong></td>
         </tr>

         <tr>
          <td>upsert</td>
          <td><span class="type">bool</span></td>
          <td>
           If <code class="parameter">filter</code> does not match an existing document,
           insert a <em class="emphasis">single</em> document. The document will be
           created from <code class="parameter">newObj</code> if it is a replacement
           document (i.e. no update operators); otherwise, the operators in
           <code class="parameter">newObj</code> will be applied to
           <code class="parameter">filter</code> to create the new document.
          </td>
          <td><strong><code>false</code></strong></td>
         </tr>

        </tbody>
       
      </table>

     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb-driver-bulkwrite.update-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-bulkwrite.update-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-bulkwrite.update-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.7.0</td>
       <td>
        Added the <code class="literal">&quot;hint&quot;</code> option.
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.6.0</td>
       <td>
        The <code class="parameter">newObj</code> parameter now accepts an aggregation
        pipeline. This feature requires MongoDB 4.2+ and will result in an
        exception at execution time if specified for an older server version.
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.5.0</td>
       <td>
        Using the <code class="literal">&quot;arrayFilters&quot;</code> option will result in an
        exception at execution time if unsupported by the server. Previously,
        no exception would be thrown and the option may have been ignored.
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.4.0</td>
       <td>
        Added the <code class="literal">&quot;arrayFilters&quot;</code> option.
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.2.0</td>
       <td>
        Added the <code class="literal">&quot;collation&quot;</code> option.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-bulkwrite.update-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1316">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\Driver\BulkWrite::update()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$bulk&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">BulkWrite</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">update</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'$set'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[</span><span style="color: #DD0000">'y'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">]],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'multi'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'upsert'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">]<br />);<br /><br /></span><span style="color: #0000BB">$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeBulkWrite</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.collection'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-mongodb-driver-bulkwrite.update-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="methodname"><a href="mongodb-driver-manager.executebulkwrite.html" class="methodname" rel="rdfs-seeAlso">MongoDB\Driver\Manager::executeBulkWrite()</a> - Execute one or more write operations</span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-driver-writeresult.html" class="classname">MongoDB\Driver\WriteResult</a></span></li>
  </ul>
 </div>


</div></div></div></body></html>